package cn.iocoder.yudao.module.member.controller.admin.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import cn.iocoder.yudao.module.system.api.tenant.TenantUserApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -运营端- 运营管理")
@RestController
@RequestMapping("/member/operate")
@Validated
public class OperateUserController {
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private TenantUserApi tenantUserApi;

    @Resource
    private CompanyService companyService;

    @PostMapping("/user-create")
    @ApiOperation("创建运营子账号用户信息")
    public CommonResult<Long> createCompanyUser(@RequestBody UserCreateReqDTO createReqVO) {
        return success(adminUserApi.createCompanySonUser(createReqVO, 2l));
    }

    @GetMapping("/getUserCountByMobile")
    @ApiOperation("根据手机号判断是否注册")
    public CommonResult<Long> getUserCountByMobile(@RequestParam String mobile) {
        return success(adminUserApi.getUserCountByMobile(mobile));
    }

    @PostMapping("/user-page")
    @ApiOperation("获得运营子用户分页")
    public CommonResult<PageResult<UserPageItemRespDTO>> getCompanyUserPage(@RequestBody UserPageReqDTO pageVO) {
        return success(adminUserApi.getOperateUsersPage(pageVO));
    }
//    @PostMapping("/user-get")
//    @ApiOperation("获得企业用户")
//    public CommonResult<CompanyUserRespVO> getCompanyUserPage() {
//        AdminUserRespDTO adminUserRespDTO= adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
//        EnterpriseCompanyDO enterpriseCompanyDO=companyService.getCompany(SecurityFrameworkUtils.getLoginUser().getCompanyId());
//        CompanyUserRespVO companyUserRespVO=new CompanyUserRespVO();
//        BeanUtils.copyProperties(enterpriseCompanyDO,companyUserRespVO);
//        companyUserRespVO.setTelephony(adminUserRespDTO.getTelephony());
//        companyUserRespVO.setEmail(adminUserRespDTO.getEmail());
//        companyUserRespVO.setUserId(SecurityFrameworkUtils.getLoginUserId());
//        companyUserRespVO.setDeptName(adminUserRespDTO.getDeptName());
//        companyUserRespVO.setRemark(adminUserRespDTO.getRemark());
//        companyUserRespVO.setNickname(adminUserRespDTO.getNickname());
//        companyUserRespVO.setJob(adminUserRespDTO.getPosition());
//        return success(companyUserRespVO);
//    }

    @PutMapping("/user-update")
    @ApiOperation("更新运营子用户信息")
    public CommonResult<Boolean> updateCompanyUser( @RequestBody UserUpateReqDTO updateReqVO) {
        adminUserApi.updateSonUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/user-delete")
    @ApiOperation("删除运营子用户信息")
    public CommonResult<Boolean> deleteCompanyUser(@RequestParam("id") Long id) {
        adminUserApi.deleteUser(id);
        tenantUserApi.deleteTenant(id);
        AdminUserRespDTO adminUserRespDTO=adminUserApi.getUser(id);
        Long  companyId=adminUserRespDTO.getCompanyId();
       if(companyId!=null){
           companyService.deleteCompany(companyId);
       }
        return success(true);
    }
}
